// pages/my/addCustomGoods/addCustomGoods.js
import * as appConfig from '../../../app-config.js'
var app = getApp()
Page({
	data: {
		verList:['',''],
    versionName:'',
    agentId:'',
    isSaved:false
  },
  onLoad: function(options) {
    app.getMemInfo().then(res=>{
      this.setData({
        agentId:res.data.agentId
      })
    })
  },
  bindVerControl(e){
    let index = e.currentTarget.dataset.index
    let verList = this.data.verList
  		//添加删除自定义规格分类
  		if (index==this.data.verList.length-1) {
  			console.log('这是最后一个')
  			verList.push('')
  		}else{
  			verList.splice(index,1)
  		}
  		this.setData({
  			verList
  		})
  	},
  	//获取input的内容
  	bindEntryVer(e){
  		console.log(e.detail.value)
  		let index = e.currentTarget.dataset.jndex
  		let ver = e.detail.value
  		this.setData({
  			['verList['+index+']']:ver
  		})
  	},
    getVerName(e){
      this.setData({
        versionName:e.detail.value
      })
    },
    saveSpecType(){
      this.setData({
        isSaved:true
      })
      let that = this
      let verList = this.data.verList
      let values = ''
      verList.forEach((item,index)=>{
        values += item+','
      })
      let data = {
        versionName:this.data.versionName,
        values:values.substr(0,values.length-1),
        agentId:this.data.agentId
      }
      console.log(data)
      app.request('wx_app/custom_merchandise/addVersion',data,'GET').then(res=>{
        console.log(res)
        if (res.code==1) {
          wx.showToast({
            title:'添加成功'
          })
          setTimeout(function(){
            wx.navigateBack({
              delta:1
            })
          },1500)
        }else{
          app.errorMsg(res.message)
          this.setData({
            isSaved:false
          })
        }
      })
    }
  })

